const app = getApp(),url = app.globalData.url;
Page({

  /**
   * 页面的初始数据
   */
  data: {
		industryArr:[],
		industryId:'',
		workYears:[],
		workYearsId: '',
		xueliArr:[],//学历
		xueliId:'',//学历
		sexList: [
			{ id: '', label: '不限' },
			{ id: 1, label: '男' },
			{ id: 2, label: '女' },
		],
		sexId: '',
		ageList: [
			{ id: '', label: '不限' },
			{ id: 1, label: '18-25' },
			{ id: 2, label: '26-35' },
			{ id: 3, label: '36-45' },
			{ id: 4, label: '46-55' },
			{ id: 5, label: '55以上' },
		],
		ageId: '',
		searchForm:{
			ageBegin: "",
      ageEnd: "",
      education: "",
      experienceBegin: "",
      experienceEnd: "",
      name: "",
      sex: "",
      specialty: "",
		},
  },

  onLoad: function (options) {
		this.cacheForm = {...this.data.searchForm}
		let cacheFilter = wx.getStorageSync('rc_filter')
		this.setData({
			...cacheFilter
		})
		this.getIndustry()
		this.getWorkYears()
		this.getXueLi()
	},
	
	getIndustry(){
		// 所属行业 industry 
		let _this = this
		wx.request({
			url: url + 'occupation/getOccupationType',
			method:'get',
			data: {},
			success: function(res){
				_this.setData({
					industryArr: [{id:'', name: '不限'}, ...res.data.data],
				})
			},
			fail:function(res){
				console.log(res)
			}
		})
	},
	// 所属行业 选择
	onIndustryChange(e){
		let { id, name } = e.currentTarget.dataset
		this.data.searchForm.specialty = name == '不限' ? '' : name
		this.setData({ industryId:id })
	},

	getWorkYears(){
		let _this = this
		wx.request({
			url: url + 'dict/getWorkYearState',
			method:'get',
			data: {},
			success: function(res){
				_this.setData({
					workYears: res.data.data,
				})
			},
			fail:function(res){
				console.log(res)
			}
		})
	},
	onWorkYearChange(e){
		let { id, name } = e.currentTarget.dataset

		let begin = '', end = ''
		if(name == '在校生/应届生') {
			begin = 0
			end = 1
		} else {
			begin = parseInt(name.split('-')[0])
			end = parseInt(name.split('-')[1])
			begin = isNaN(begin) ? '' : begin
			end = isNaN(end) ? '' : end
		}
		this.data.searchForm.experienceBegin = begin
		this.data.searchForm.experienceEnd = end
		this.setData({ workYearsId:id })
	},
	getXueLi(){
		let _this = this
		wx.request({
			url: url + 'dict/getEducationState',
			method:'get',
			data: {},
			success: function(res){
				_this.setData({
					xueliArr:res.data.data,
				})
			},
			fail:function(res){
				console.log(res)
			}
		})
	},
	//学历
	xuelixueli(e){
		let { id, name } = e.currentTarget.dataset
		this.data.searchForm.education = name
		this.setData({ xueliId:id})
	},
	onSexChange(e){
		let {id} = e.currentTarget.dataset
		this.data.searchForm.sex = id
		this.setData({
			sexId: id
		})
	},
	onAgeChange(e){
		let {id, name} = e.currentTarget.dataset
		let ageBegin = '', ageEnd = ''
		ageBegin = parseInt(name.split('-')[0])
		ageEnd = parseInt(name.split('-')[1])
		ageBegin = isNaN(ageBegin) ? '' : ageBegin
		ageEnd = isNaN(ageEnd) ? '' : ageEnd
		this.data.searchForm.ageBegin = ageBegin
		this.data.searchForm.ageEnd = ageEnd
		this.setData({
			ageId: id
		})
	},

	
	reset(){
		this.setData({
			searchForm: {...this.cacheForm},
			industryId: '',
			workYearsId:'',
			xueliId: '',
			sexId: '',
			ageId: ''
		})
		wx.removeStorage('rc_search')
		wx.removeStorage('rc_filter')
	},
	submit(){
		wx.setStorageSync('rc_search', this.data.searchForm)
		let cacheFilter = {
			industryId: this.data.industryId,
			workYearsId: this.data.workYearsId,
			xueliId: this.data.xueliId,
			sexId: this.data.sexId,
			ageId: this.data.ageId
		}
		wx.setStorageSync('rc_filter', cacheFilter)
		let pages = getCurrentPages();//当前页面栈
		let prePage = pages[pages.length - 2]
		prePage.reload()
		wx.navigateBack()
	},





})